﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.Company
{
    public static class CompanyContactManager
    {
        public static CompanyContact GetContact(string _userName)
        {
            return DBHelper.Get<CompanyContact>(c => c.CompanyUserName == _userName, "获取公司联系信息出错：");
        }
        public static void SaveContact(
            string _userName,
            string _address,
            string _contact,
            string _email,
            string _tel,
            string _website
            )
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_contact)) throw new Exception("联系人不能为空");
                if (string.IsNullOrEmpty(_tel)) throw new Exception("电话号码不能为空");
                if (string.IsNullOrEmpty(_email)) throw new Exception("联系人电子邮件不能为空");
                if (string.IsNullOrEmpty(_address)) throw new Exception("公司联系地址不能为空");
                if (string.IsNullOrEmpty(_userName)) throw new Exception("用户名不能为空");

                if (_userName.Length > 50) throw new Exception("用户名不能超过50个字符");
                if (_address.Length > 150) throw new Exception("联系地址不能超过150个字符");
                if (_contact.Length > 50) throw new Exception("联系人姓名不能超过50个字符");
                if (_email.Length > 50) throw new Exception("联系人电子邮件不能超过50个字符");
                if (_tel.Length > 15) throw new Exception("联系电话不能超过15个字符");
                if (!string.IsNullOrEmpty(_website) && _website.Length > 50) throw new Exception("公司网址不能超过50个字符");

                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _c = _adc.CompanyContact.Where(c => c.CompanyUserName == _userName).FirstOrDefault();

                    if (_c == null)
                    {
                        _c = new CompanyContact();
                        _c.ID = Guid.NewGuid();
                        _c.CompanyUserName = _userName;
                        _c.Address = _address;
                        _c.Contact = _contact;
                        _c.Email = _email;
                        _c.Tel = _tel;
                        _c.WebSite = _website;

                        _adc.CompanyContact.InsertOnSubmit(_c);
                    }
                    else
                    {
                        _c.Address = _address;
                        _c.Contact = _contact;
                        _c.Email = _email;
                        _c.Tel = _tel;
                        _c.WebSite = _website;
                    }
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存企业联系方式出错：" + _e.Message);
            }
        }

        public static bool Delete(string _user)
        {
            //var _c = GetContact(_user);
            //return DBHelper.Delete(_c,"删除企业联系信息出错：");
            throw new NotImplementedException();
        }
    }
}
